<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="/taglibs.jsp"%>
		<!-- 月历 -->
		<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick.js"></script>
		<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick-zh-CN.js"></script>
		<link rel="stylesheet" type="text/css" href="/css/smoothness.datepick.css"/>
		<!-- 時間 -->	
		<script type="text/javascript" src="/js/jquery.timePicker.js"></script>
		<link rel="stylesheet" type="text/css" href="/css/timePicker.css"/>
		<!-- 验证 -->
		<script type="text/javascript" charset="UTF-8" src="/js/formValidator_min.js" charset="UTF-8"></script>
		<script type="text/javascript" charset="UTF-8" src="/js/formValidator.js" charset="UTF-8"></script>
		<script type="text/javascript" charset="UTF-8" src="/js/formValidatorRegex.js" charset="UTF-8"></script>
		<!-- 数字验证 -->
		<script type="text/javascript" charset="UTF-8" src="/js/jquery.alphanumeric.js"></script>
		<script type="text/javascript" charset="UTF-8" src="/js/yaodian100.common.js"></script>
		<script language="javascript">
		$().ready( function() {

			
			$('#startDateParam').datepick({minDate: new Date(${today}), showOn: 'both',numberOfMonths:2, 
		    	buttonImageOnly: true, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});
			$('#endDateParam').datepick({minDate:new Date(${minDate}), showOn: 'both',numberOfMonths:2,beforeShow:customRange, 
		    	buttonImageOnly: true, buttonImage: '<%=ServerValue.getMidContextPath()%>/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-50:+50'});

			function customRange (){
				var startDt = $('#startDateParam').val(); //起始日
				var endDt = new Date(parseInt(startDt.substring(0,4),10),parseInt(startDt.substring(5,7),10)-1,parseInt(startDt.substring(8,10),10)); //結束日
				return {minDate:endDt}; 
			}

			$("#timestart").timePicker();
			$("#timeend").timePicker();

			$("#editMainProductIdParam").numeric();
			 $("#editAddonProductIdParam").numeric();
			 $("#editAddOnPrice").numeric({allow:"."});
			$("#editMainProductIdParam").blur(function(){
				$(this).blur(function(){
					$(this).val(prodPadLeft($(this).val()));
				});
			});
			$("#editAddonProductIdParam").blur(function(){
				$(this).blur(function(){
					$(this).val(prodPadLeft($(this).val()));
				});
			});
			if($("#editMainProductIdParam").val()!=""){
				getProName($("#editMainProductIdParam").val(),'main',$("#showProductName"),$("#editMainProductIdParam").val());
			}
			if($("#addonId").val()!=""){
			 $('#editMainProductIdParam').attr("disabled", true); 
			}
			//編輯時select選取反白要修改的商品
			if($("#addonId").val()!=""){
				$('#addOnSelect option').each(function(){					
					if(this.value.split(",")[0]==$("#addonId").val()){	
						$(this).attr("selected","true"); 
					}									
				});
			}
			
			$("#findProductName").click(function(){
				if($("#editMainProductIdParam").val()!=""){
					getProName($("#editMainProductIdParam").val(),'main',$("#showProductName"),$("#editMainProductIdParam").val());
				}
			});

			clear_form_elements(this);
			
			$("#findAddOnProductName").click(function(){
				var getMainProductName=$("#showProductName").text().split("，")[0];
				if(getMainProductName=='此商品非一般商品'){
					alert('主商品非一般商品，请先选择主商品');
					return false;
				}
				if ($("#editAddonProductIdParam").val() == "" ){
					alert("请输入次商品编号");
					return false;
				}
				if ($("#showProductName").text() == "请查看商品SCM状态，SCM转单商品不能搭售"){
					alert("请查看商品SCM状态，SCM转单商品不能搭售");
					return false;
				} 
				if ($("#editAddonProductIdParam").val() == $("#editMainProductIdParam").val() ){
					alert("主商品不可和次商品编号相同");
					return false;
				}
				if($("#editAddonProductIdParam").val()!=""){
					getProName($("#editAddonProductIdParam").val(),'sub',$("#showAddOnProductName"),$("#editMainProductIdParam").val());
				}
			});

			//點選加入
			$("#addSelect").click(function(){
				if($("#editAddonProductIdParam").val()!=""){
					getProNameByAddSelete($("#editAddonProductIdParam").val(),'sub',$("#showAddOnProductName"),$("#editMainProductIdParam").val());
				}
			})
	        
			$("#removeSelect").click(function(){
					if(confirm("确定删除吗?")==true){
						var vAddonId=$("#addOnSelect option:selected").attr("value").split(",")[0];
						if(vAddonId!=""){
							$.getJSON("<%=ServerValue.getMidContextPath()%>/addonproduct/del.do?type=json", 
									{addonId : vAddonId}
								,function(productNameDataExist){
							});
							$("#addOnSelect option:selected").remove();
							clear_form_elements(this.form);
						}else{
							$("#addOnSelect option:selected").remove();	
							clear_form_elements(this.form);
						}	 				
					}else{
						return false;				
					}
							
			});		

			//點選select裏的item
			$("#addOnSelect").click(function(){					
				$("#addonId").val($("#addOnSelect option:selected").attr("value").split(",")[0]);
				$("#tmpAddonId").val($("#addOnSelect option:selected").attr("value").split(",")[0]);			
				$("#editMainProductIdParam").val($("#addOnSelect option:selected").attr("value").split(",")[1]);
				$("#editAddonProductIdParam").val($("#addOnSelect option:selected").attr("value").split(",")[2]);
				$("#editAddOnPrice").val($("#addOnSelect option:selected").attr("value").split(",")[3]);
				$("#startDateParam").val($("#addOnSelect option:selected").attr("value").split(",")[4].split(" ")[0]);
				$("#timestart").val($("#addOnSelect option:selected").attr("value").split(",")[4].split(" ")[1]);
				$("#endDateParam").val($("#addOnSelect option:selected").attr("value").split(",")[5].split(" ")[0]);
				$("#timeend").val($("#addOnSelect option:selected").attr("value").split(",")[5].split(" ")[1]);
				$("#editAddOnDesc").val($("#addOnSelect option:selected").attr("value").split(",")[7]);

				if($("#addOnSelect option:selected").attr("value").split(",")[8]=="00"){
					$("#editApplyMemberRank1").attr("checked","checked");
				}
				if($("#addOnSelect option:selected").attr("value").split(",")[8]=="10"){					
					$("#editApplyMemberRank2").attr("checked","checked");
				}
				if($("#addOnSelect option:selected").attr("value").split(",")[8]=="50"){
					$("#editApplyMemberRank3").attr("checked","checked");
				}
				if($("#addOnSelect option:selected").attr("value").split(",")[8]=="90"){
					$("#editApplyMemberRank4").attr("checked","checked");
				}
				if($("#addOnSelect option:selected").attr("value").split(",")[6]=="Y"){
					$("#editIsEnable1").attr("checked","checked");
				}
				if($("#addOnSelect option:selected").attr("value").split(",")[6]=="N"){
					$("#editIsEnable2").attr("checked","checked");
				}
				getProName($("#addOnSelect option:selected").attr("value").split(",")[2],'sub',$("#showAddOnProductName"),$("#editMainProductIdParam").val());
			});	

			
			$("#submit_button").click(function(){
				var addData="";				
				$('#addOnSelect option').each(function(){
					addData=addData+this.value+";";										
				});
				if (addData == "" ){
					alert("请加入");
					return false;
				}
				$("#finalAddOnData").val(addData);
			});
			
		});

		function validateData(){
			if($("#showAddOnProductName").text()=='搭售商品只能有一個規格'){
				alert('搭售商品只能有一個規格');
				return false;
			}
			if($("#showAddOnProductName").text()=='请查看商品SCM状态，SCM转单商品不能搭售'){
				alert('请查看商品SCM状态，SCM转单商品不能搭售');
				return false;
			}
			
			if($("#showAddOnProductName").text().substring(0,8)=='此商品非一般商品'){
				alert('此商品非一般商品，无法提供搭售设定');
				return false;
			}

			if($("#showAddOnProductName").text().indexOf("此商品为限定立即购买商品") != -1){
				alert('此商品为限定立即购买商品，无法提供搭售设定');
				return false;
			}

			if($("#showAddOnProductName").text()=='搭售商品已被删除'){
				alert('搭售商品已被删除');
				return false;
			}
			
			if($("#showAddOnProductName").text()=='搭售商品的销售状态为可查询不可销售'){
				alert('搭售商品的销售状态为可查询不可销售');
				return false;
			}				
			var getMainProductName=$("#showProductName").text().split("，")[0];
			if(getMainProductName=='此商品非一般商品'){
				alert('主商品非一般商品，请先选择主商品');
				return false;
			}
			if ($("#showProductName").text() == "请查看商品SCM状态，SCM转单商品不能搭售"){
				alert("请查看商品SCM状态，SCM转单商品不能搭售");
				return false;
			} 
			if ( $("#showAddOnProductName").text()=='查无商品资料(或无该权限查看)'){
				alert('查无商品资料(或无该权限查看)');
				return false;
			}
			if ($("#editMainProductIdParam").val() == "" ){
					alert("请输入主商品编号");
					return false;
			}
			if ($("#editAddonProductIdParam").val() == "" ){
					alert("请输入次商品编号");
					return false;
			}
			if ($("#showAddOnProductName").text() == "" ){
					alert("次商品名称不得为空");
					return false;
			}
			if ($("#editAddOnDesc").val() == "" ){
				alert("简称不得为空");
				return false;
			}
			if ($("#editAddOnPrice").val() == "" ){
				alert("请输入搭配售价");
				return false;
			}			 	
			if ($("#startDateParam").val() == "" ){
					alert("请输入開始日期");
					return false;
			}
	    	if ($("#timestart").val() == "" ){
				alert("请输入開始時間");
				return false;
			}
	    	if ($("#endDateParam").val() == "" ){
				alert("请输入結束日期");
				return false;
			}
	    	if ($("#timeend").val() == "" ){
				alert("请输入結束時間");
				return false;
			}
	    	if ($('#editApplyMemberRank1').is(":checked") == false && $('#editApplyMemberRank2').is(":checked") == false&& $('#editApplyMemberRank3').is(":checked") == false&& $('#editApplyMemberRank4').is(":checked") == false){
				alert("请输入适用会员等级 ");
				return false;
			}
	    	if ($('#editIsEnable1').is(":checked") == false && $('#editIsEnable2').is(":checked") == false){
				alert("请输入活动状态 ");
				return false;
			}
			//validate end				
			var addOnDataText = "待处理,"+$("#editAddonProductIdParam").val()
							+","+$("#showAddOnProductName").html()
							+",("+$("#editAddOnPrice").val()+")"
							+",("+$("#startDateParam").val()+" "+$("#timestart").val()+")-("
							+$("#endDateParam").val()+" "+$("#timeend").val()+")";
			
			var addOnDataValue = "";
			//alert($("#addonId").val());
			if($("#addonId").val()==""){//新增的加入button
				//alert('新增的加入button');
				//alert($("#addonId").val());
				
				addOnDataValue=","+$("#editMainProductIdParam").val()
				+","+$("#editAddonProductIdParam").val()
				+","+$("#editAddOnPrice").val()
				+","+$("#startDateParam").val()+" "+$("#timestart").val()
				+","+$("#endDateParam").val()+" "+$("#timeend").val();
				if($("#editIsEnable1").attr("checked")){
					addOnDataValue=addOnDataValue+","+$("#editIsEnable1").attr("value");
				}
				if($("#editIsEnable2").attr("checked")){
					addOnDataValue=addOnDataValue+","+$("#editIsEnable2").attr("value");
				}					
				addOnDataValue=addOnDataValue+","+$("#editAddOnDesc").val();
				
				if($("#editApplyMemberRank1").attr("checked")){					
					addOnDataValue=addOnDataValue+","+$("#editApplyMemberRank1").attr("value");
				}
				if($("#editApplyMemberRank2").attr("checked")){
					addOnDataValue=addOnDataValue+","+$("#editApplyMemberRank2").attr("value");
				}
				if($("#editApplyMemberRank3").attr("checked")){
					addOnDataValue=addOnDataValue+","+$("#editApplyMemberRank3").attr("value");
				}
				if($("#editApplyMemberRank4").attr("checked")){
					addOnDataValue=addOnDataValue+","+$("#editApplyMemberRank4").attr("value");
				}		
				
				
				$('#addOnSelect').append($("<option></option>").attr("value",addOnDataValue).text(addOnDataText));
			}else{//修改的加入button					
				addOnDataValue=$("#tmpAddonId").attr("value")
				+","+$("#editMainProductIdParam").val()
				+","+$("#editAddonProductIdParam").val()
				+","+$("#editAddOnPrice").val()
				+","+$("#startDateParam").val()+" "+$("#timestart").val()
				+","+$("#endDateParam").val()+" "+$("#timeend").val();
				if($("#editIsEnable1").attr("checked")){
					addOnDataValue=addOnDataValue+","+$("#editIsEnable1").attr("value");
				}
				if($("#editIsEnable2").attr("checked")){
					addOnDataValue=addOnDataValue+","+$("#editIsEnable2").attr("value");
				}
				addOnDataValue=addOnDataValue+","+$("#editAddOnDesc").val()	;				
				if($("#editApplyMemberRank1").attr("checked")){					
					addOnDataValue=addOnDataValue+","+$("#editApplyMemberRank1").attr("value");
				}
				if($("#editApplyMemberRank2").attr("checked")){
					addOnDataValue=addOnDataValue+","+$("#editApplyMemberRank2").attr("value");
				}
				if($("#editApplyMemberRank3").attr("checked")){
					addOnDataValue=addOnDataValue+","+$("#editApplyMemberRank3").attr("value");
				}
				if($("#editApplyMemberRank4").attr("checked")){
					addOnDataValue=addOnDataValue+","+$("#editApplyMemberRank4").attr("value");
				}		
				$("#addOnSelect option:selected").val(addOnDataValue);
				$("#addOnSelect option:selected").text(addOnDataText);
			}								
			clear_form_elements(this.form);
		}
		function clear_form_elements(ele) {
		    $(ele).find(':input').each(function() {
		        switch(this.type) {
		            case 'password':
		            case 'select-multiple':
		            case 'select-one':
		            case 'text':
		            case 'textarea':
			            if(this.id!="editMainProductIdParam"){
		                $(this).val('');
		                break;
			            }
		            case 'checkbox':
		        }
		    }); 
			$("#showAddOnProductName").text("");
			$("#addonId").val("");
			$("#dMainPrice").html("");
			$("#dSubPrice").html("");
		}
		//取得商品名称
		function getProName(prodid,flag,showName,markMainProductId) {
			$.getJSON("<%=ServerValue.getMidContextPath()%>/addonproduct/findProductName.do?type=json", 
					{mainProductId : prodid,mainSubFlag:flag,markMainProductId:markMainProductId}
				,function(productNameDataExist){
					showName.html(productNameDataExist);
			});
		}	
		//取得商品名称(按加入鈕)
		function getProNameByAddSelete(prodid,flag,showName,markMainProductId) {
			$.getJSON("<%=ServerValue.getMidContextPath()%>/addonproduct/findProductName.do?type=json", 
					{mainProductId : prodid,mainSubFlag:flag,markMainProductId:markMainProductId,editAddOnPrice:$("#editAddOnPrice").val()}
				,function(productNameDataExist){
					setTimeout(function(){showName.html(productNameDataExist);
					validateData();
					},1);
			});
		}				
		</script>	
		
		<style type="text/css" media="screen">
		    <!--      
		     SELECT.selCont { margin: 10px; width: 700px; }
		    -->
	    </style>
<style type="text/css">
.msg{float:left; line-height:1em; margin:0; display:block;border:1px solid #f66;padding:2px 5px;*padding:2px 5px 1px;}
.msg.rn{background-color:#fff2f2;border-color:#f66;}
</style>
<s:if test="hasActionErrors()">
	<s:actionerror cssClass="msg rn"/>
</s:if>
<s:if test="hasActionMessages()">
	<s:actionmessage cssClass="msg rn"/>
</s:if>
<p></p>
<h1>搭售商品管理(加價購/贈品/加一元多一件)<br/><div class="f-link"><a href="<%=ServerValue.getMidContextPath()%>/addonproduct/list.do">回列表页</a></div></h1>

<s:form action="save.do" method="post">
	<table class="tb-data" width="100%">
		<tr>
			<th>主商品编号</th>
		</tr>
		<tr>
			<td><input type="text" name="editMainProductIdParam"
				id="editMainProductIdParam" size="8" value="${addOnProduct.mainProductId}" />
				<input type="button" id="findProductName" value="查询商品名称" />
				<span id="showProductName"></span>
				</td>
		</tr>
		
		<tr>
			<th>次商品资讯</th>
		</tr>
		<tr>
			<td>商品编号:<input type="text" name="editAddonProductIdParam" id="editAddonProductIdParam" size="8" value="" />
			<input type="button" id="findAddOnProductName" value="查询次商品名称" />
				<span id="showAddOnProductName"></span>
			<br/>简称:<input type="text" name="editAddOnDesc" id="editAddOnDesc" size="40" maxlength="20" value="" /> 长度20</td>
		</tr>
		<tr>
			<td>商品搭配售价:<input type="text" name="editAddOnPrice" id="editAddOnPrice" size="8" value="0" /><span class="txt-rd">【※商品搭配售价小数点1位数后无条件舍去】</span> 
			<c:if test="${shelfMain.shelfPrice !=null}">
			<fmt:formatNumber pattern="#,###,##0.0" var="mainShelfPrice" value="${shelfMain.shelfPrice}"/>
			<span id="dMainPrice">主商品售价：${mainShelfPrice}</span>
			</c:if>
			<c:if test="${mainProduct.productCost !=null}">
			<fmt:formatNumber pattern="#,###,##0.0" var="mainProductCost" value="${mainProduct.productCost}"/>
			<span id="dMainCost">主商品成本：${mainProductCost}</span>
			</c:if>
			<c:if test="${shelfSub.shelfPrice !=null}">
			<fmt:formatNumber pattern="#,###,##0.0" var="subShelfPrice" value="${shelfSub.shelfPrice}"/>
			<span id="dSubPrice">次商品售价：${subShelfPrice}</span>
			</c:if>
			<c:if test="${subProduct.productCost !=null}">
			<fmt:formatNumber pattern="#,###,##0.0" var="subProductCost" value="${subProduct.productCost}"/>
			<span id="dSubCost">次商品成本：${subProductCost}</span>
			</c:if>
			<br/>#若价格设为0,即表示该商品为赠品
			<br/>#若价格设为1,即表示加一元多一件
			</td>
		</tr>
		<tr>
			<td>活动开始时间:  <input type="text" name="startDateParam" id="startDateParam" size="20" value=""/>	       
        		 <c:if test="${addOnProduct.addonId !=null}">
        		<input id="timestart" name="timestart" type="text" value=""  size="10"/>
        		</c:if>
        		<c:if test="${addOnProduct.addonId ==null}">
        		<input id="timestart" name="timestart" type="text" size="10"/>
        		</c:if>   
			<br/>活动结束时间:  <input type="text" name="endDateParam" id="endDateParam" size="20" value=""/>	
			     <c:if test="${addOnProduct.addonId !=null}">
			    <input id="timeend" name="timeend" type="text" value="" size="10"/> 
			     </c:if>
        		<c:if test="${addOnProduct.addonId ==null}">
        		<input id="timeend" name="timeend" type="text" size="10"/> 
        		</c:if>
			</td>
		</tr>
		<tr>
			<td>适用会员等级:
			 <input id="editApplyMemberRank1" name="editApplyMemberRank" type="radio" value="00" <c:if test="${empty addOnProduct.applyMemberRank or addOnProduct.applyMemberRank eq '00'}">checked="checked"</c:if>/>一般会员
                    <input id="editApplyMemberRank2" name="editApplyMemberRank" type="radio" value="10" <c:if test="${addOnProduct.applyMemberRank eq '10'}">checked="checked"</c:if>/>银卡
                    <input id="editApplyMemberRank3" name="editApplyMemberRank" type="radio" value="50" <c:if test="${addOnProduct.applyMemberRank eq '50'}">checked="checked"</c:if>/>金卡
                    <input id="editApplyMemberRank4" name="editApplyMemberRank" type="radio" value="90" <c:if test="${addOnProduct.applyMemberRank eq '90'}">checked="checked"</c:if>/>钻石卡
			</td>
		</tr>
		<tr>
			<td>活动状态:
			 <input id="editIsEnable1" name="editIsEnable" type="radio" value="Y" <c:if test="${empty addOnProduct.isEnable or addOnProduct.isEnable eq 'Y'}">checked="checked"</c:if>/>启用
                    <input id="editIsEnable2" name="editIsEnable" type="radio" value="N" <c:if test="${addOnProduct.isEnable eq 'N'}">checked="checked"</c:if>/>关闭
			</td>
		</tr>
		<tr><td><input onclick="clear_form_elements(this.form)" type="button" value="清除" />	
				<input type="hidden" id="tmpAddonId" value="${addonId}"/>
		</td></tr>
		<tr>
			<td>
			<div class="f-link"><!-- <a href="#" id="editedToSelect">修改</a> --><a href="#" id="addSelect">加入</a><a href="#" id="removeSelect">移除</a></div>
			<br/>搭售商品列表:
			<!-- 新增時顯示的select -->
			<c:if test="${addonId ==null}">
					<select id="addOnSelect" name="addOnSelect" size="10" class="selCont">		               
		            </select>
		     </c:if>
		     <!-- 修改時顯示的select -->
		     <c:if test="${addonId !=null}">
		     		<br/>次商品<br/>
		            <select id="addOnSelect" name="addOnSelect" size="10" class="selCont">	
		            <c-rt:forEach items="${addOnProductsByMainId}" var="addonproduct" varStatus="rows_index">
		            <option value="${addonproduct[0]},${addonproduct[1]},${addonproduct[3]},${addonproduct[5]},${addonproduct[6]},${addonproduct[7]},${addonproduct[8]},${addonproduct[9]},${addonproduct[10]},${addonproduct[11]},S1">未开始,${addonproduct[3]},${addonproduct[4]},$${addonproduct[5]}</option>
		            </c-rt:forEach>
		            
		            <c-rt:forEach items="${addOnProductsByMainId2}" var="addonproduct2" varStatus="rows_index">
		            <option value="${addonproduct2[0]},${addonproduct2[1]},${addonproduct2[3]},${addonproduct2[5]},${addonproduct2[6]},${addonproduct2[7]},${addonproduct2[8]},${addonproduct2[9]},${addonproduct2[10]},${addonproduct2[11]},S2">進行中,${addonproduct2[3]},${addonproduct2[4]},$${addonproduct2[5]}</option>
		            </c-rt:forEach>
		            
		            <c-rt:forEach items="${addOnProductsByMainId3}" var="addonproduct3" varStatus="rows_index">
		            <option value="${addonproduct3[0]},${addonproduct3[1]},${addonproduct3[3]},${addonproduct3[5]},${addonproduct3[6]},${addonproduct3[7]},${addonproduct3[8]},${addonproduct3[9]},${addonproduct3[10]},${addonproduct3[11]},S3">	已結束,${addonproduct3[3]},${addonproduct3[4]},$${addonproduct3[5]}</option>
		            </c-rt:forEach>	               
		            </select>
		                        
		            
		       </c:if>
			</td>
		</tr>		
		<tr>
			<td>
			<input type="hidden" id="addonId" name="addonId" value="${addonId}" />
			<input type="hidden" name="finalAddOnData" id="finalAddOnData" value=""/>
			<input type="hidden" name="tmpSelectIndex" id="tmpSelectIndex" value=""/>
			<input type="submit" id="submit_button" value="储  存" />								
			</td>
		</tr>
	</table>
</s:form>
